<!--
 * @Description: 
 * @version: 
 * @Author: dh
 * @Date: 2021-08-25 09:50:37
 * @LastEditors: dh
 * @LastEditTime: 2022-02-23 16:58:34
-->
<!-- B.html -->
<!doctype html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>B</title>
	</head>
	<style>
		body {
			border: 1px solid red;
		}
	</style>

	<body>
		<h1>B 页面</h1>
		<button onclick="send2A()">发消息给A</button>
		<script>
			window.onmessage = function (e) {
				console.log(e.data); //我爱你
				e.source.postMessage('我不爱你', e.origin);
			};
			function send2A() {
				window.parent.postMessage('我爱你', 'http://127.0.0.1:5500'); //发送数据,指定哪些窗口能接收到消息事件，其值可以是字符串"*"（表示无限制）或者一个URI
			}
		</script>
	</body>
</html>
